package com.yy.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.yy.domain.ResponseResult;
import com.yy.domain.User;
import com.yy.dto.LoginDTO;
import com.yy.dto.MessageDTO;
import com.yy.service.LoginServcie;
import com.yy.vo.LoginVO;
import com.yy.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "登陆相关接口")
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private LoginServcie loginServcie;

    @ApiOperation("用户登陆")
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user) {
        //登录
        return loginServcie.login(user);
    }
    @ApiOperation("用户信息")
    @GetMapping("/user/getInfo")
    public ResponseResult getInfo() {
        return loginServcie.getInfo();
    }
    @ApiOperation("用户登出")
    @GetMapping("/user/logout")
    public ResponseResult logout() {
        return loginServcie.logout();
    }

    /**
     * 获取短信验证码
     *
     * @param messageVO
     */
    @ApiOperation("发送短信验证码")
    @PostMapping("/sendMessageAuthCode")
    public ResponseResult sendMessageAuthCode(@RequestBody MessageVO messageVO) {
        String url = "http://guahao.wchscu.cn/login/sendMessageAuthCode";
        String data = "authKey=&authCode=&phone=" + messageVO.getAppointmentPhone();
        HttpRequest request = HttpUtil.createRequest(Method.POST, url);
        request.header("Connection", "keep-alive")
                .header("Origin", "http://guahao.wchscu.cn")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X -1_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
                .header("token", "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyNTI0Mzk2QXBIZFNQT3NwZVVXNmpRSXdYbGZUUnNXcFcxVEpnZldhWTBUV3hWNnc5ZFoiLCJpYXQiOjE2NDY5MDIxMDUsInN1YiI6IntcInVzZXJJZFwiOlwiMjUyNDM5NlwiLFwiYWNjb3VudElkXCI6XCIyOTgwMzMzXCIsXCJ1c2VyVHlwZVwiOjAsXCJhcHBDb2RlXCI6XCJIWEdZQVBQXCIsXCJjaGFubmVsQ29kZVwiOlwiUEFUSUVOVF9BTkRST0lEXCIsXCJkZXZpY2VudW1iZXJcIjpcIkFwSGRTUE9zcGVVVzZqUUl3WGxmVFJzV3BXMVRKZ2ZXYVkwVFd4VjZ3OWRaXCIsXCJkZXZpY2VUeXBlXCI6XCJBUFBcIixcImFjY291bnROb1wiOlwiMTgzMjgwMDM0MzNcIixcIm5hbWVcIjpudWxsLFwiZG9jdG9ySWRcIjpudWxsLFwib3JnYW5Db2RlXCI6bnVsbH0iLCJleHAiOjE2NDk0OTQxMDV9.4oF1kMDRTHUXyZX0A6ihH8VemRxDNAyW7ynP-wPoyaQ***HXGYAPP")
                .header("Content-Type", "application/x-www-form-urlencoded;application/json;charset=UTF-8;")
                .header("Accept", "*/*")
                .header("Referer", "http://guahao.wchscu.cn/file/h5/index.html")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cookie", "acw_tc=781bad2316886240948188586e511e2b80c89998fb3a2a62e2492d387dfbde")
                .body(data);
        HttpResponse response = request.execute();
        if (response.isOk()) {
            String body = response.body();
            MessageDTO result = JSON.parseObject(body, MessageDTO.class);
            return new ResponseResult(result.getCode(), result.getMessage(), result.getData());
        } else {
            log.info("请求失败，状态码：" + response.getStatus());
            return new ResponseResult(500, "请求失败");
        }
    }

    /**
     * 获取短信验证码
     *
     * @param loginVO
     */
    @ApiOperation("华医通登陆")
    @PostMapping("/login/login")
    public ResponseResult sendMessageAuthCode(@RequestBody LoginVO loginVO) {
        String url = "http://guahao.wchscu.cn/login/login";
        String data = "authKey=" + loginVO.getAuthKey() + "&authCode=" + loginVO.getAuthCode() + "&phone=" + loginVO.getAppointmentPhone();
        HttpRequest request = HttpUtil.createRequest(Method.POST, url);
        request.header("Connection", "keep-alive")
                .header("Origin", "http://guahao.wchscu.cn")
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X -1_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
                .header("token", "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyNTI0Mzk2QXBIZFNQT3NwZVVXNmpRSXdYbGZUUnNXcFcxVEpnZldhWTBUV3hWNnc5ZFoiLCJpYXQiOjE2NDY5MDIxMDUsInN1YiI6IntcInVzZXJJZFwiOlwiMjUyNDM5NlwiLFwiYWNjb3VudElkXCI6XCIyOTgwMzMzXCIsXCJ1c2VyVHlwZVwiOjAsXCJhcHBDb2RlXCI6XCJIWEdZQVBQXCIsXCJjaGFubmVsQ29kZVwiOlwiUEFUSUVOVF9BTkRST0lEXCIsXCJkZXZpY2VudW1iZXJcIjpcIkFwSGRTUE9zcGVVVzZqUUl3WGxmVFJzV3BXMVRKZ2ZXYVkwVFd4VjZ3OWRaXCIsXCJkZXZpY2VUeXBlXCI6XCJBUFBcIixcImFjY291bnROb1wiOlwiMTgzMjgwMDM0MzNcIixcIm5hbWVcIjpudWxsLFwiZG9jdG9ySWRcIjpudWxsLFwib3JnYW5Db2RlXCI6bnVsbH0iLCJleHAiOjE2NDk0OTQxMDV9.4oF1kMDRTHUXyZX0A6ihH8VemRxDNAyW7ynP-wPoyaQ***HXGYAPP")
                .header("Content-Type", "application/x-www-form-urlencoded;application/json;charset=UTF-8;")
                .header("Accept", "*/*")
                .header("Referer", "http://guahao.wchscu.cn/file/h5/index.html")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cookie", "acw_tc=781bad2316886240948188586e511e2b80c89998fb3a2a62e2492d387dfbde")
                .body(data);
        HttpResponse response = request.execute();
        if (response.isOk()) {
            String body = response.body();
            log.info("请求成功：" + body);
            LoginDTO result = JSON.parseObject(body, LoginDTO.class);
            return new ResponseResult(result.getCode(), result.getMessage(), result.getData());
        } else {
            log.info("请求失败，状态码：" + response.getStatus());
            return new ResponseResult(500, "请求失败");
        }
    }

}
